<template>
  <el-dialog
    :title="type === 'add' ? '创建' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
    @closed="dialogClose"
  >
    <el-form
      :model="dataSource"
      :rules="dataRule"
      ref="attrGroup"
      @keyup.enter.native="saveOrUpdate()"
      label-width="120px"
    >
      <el-form-item label="组名" prop="attrGroupName">
        <el-input v-model="dataSource.attrGroupName" placeholder="组名"></el-input>
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-input v-model="dataSource.sort" placeholder="排序"></el-input>
      </el-form-item>
      <el-form-item label="描述" prop="descript">
        <el-input v-model="dataSource.descript" placeholder="描述"></el-input>
      </el-form-item>
      <el-form-item label="组图标" prop="icon">
        <el-input v-model="dataSource.icon" placeholder="组图标"></el-input>
      </el-form-item>
      <el-form-item label="所属分类" prop="catelogId">
        <category-cascader :catelogPath.sync="catelogPath"></category-cascader>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="saveOrUpdate()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import CategoryCascader from '../../common/category-cascader'
import { queryCategoryTree, threeCategoryTree } from '@/api/product/category'
import { queryByAttrId, saveAttrGroup, updateAttrGroup } from '@/api/product/attrgroup'


export default {
  data() {
    return {
      type: 'add',
      props:{
        value:"catId",
        label:"name",
        children:"children"
      },
      visible: false,
      categorys: [],
      catelogPath: [],
      dataSource: {
        attrGroupId: null,
        attrGroupName: "",
        sort: "",
        descript: "",
        icon: "",
        catelogId: null
      },
      dataRule: {
        attrGroupName: [
          { required: true, message: "组名不能为空", trigger: "blur" }
        ],
        sort: [{ required: true, message: "排序不能为空", trigger: "blur" }],
        descript: [
          { required: true, message: "描述不能为空", trigger: "blur" }
        ],
        icon: [{ required: true, message: "组图标不能为空", trigger: "blur" }],
        catelogId: [
          { required: true, message: "所属分类id不能为空", trigger: "blur" }
        ]
      }
    };
  },
  components:{CategoryCascader},

  methods: {
    dialogClose(){
      this.catelogPath = [];
    },
    async tree(){
      const { data } = await queryCategoryTree()
      this.categorys = data
    },
    init(id) {
      this.visible = true
      if (id) {
        this.type = 'edit'
        this.$nextTick(() => {
          queryByAttrId(id).then(async (res) => {
            this.dataSource = res.data
            threeCategoryTree(this.dataSource.catelogId).then(({ data }) =>{
              this.catelogPath = data
            })
          })
        })
      } else {
        this.dataSource = {
          attrGroupId: null,
          attrGroupName: "",
          sort: "",
          descript: "",
          icon: "",
          catelogId: null
        }
        this.type = 'add'
      }
    },
    // 表单提交
    saveOrUpdate () {
      this.dataSource.catelogId = this.catelogPath[2]
      this.$refs["attrGroup"].validate(valid => {
        if (valid) {
          if (this.type === 'add') {
            saveAttrGroup(this.dataSource).then(() => {
              this.reset()
            }).catch(() => {
              this.$refs.attrGroup.resetFields()
            })
          } else {
            updateAttrGroup(this.dataSource).then(() => {
              this.reset()
            }).catch(() => {
              this.$refs.attrGroup.resetFields()
            })
          }
          this.visible = false
        }
      });
    },
    reset () {
      this.$emit('refreshDataList')
      this.$refs.attrGroup.resetFields()
    },
    cancel () {
      this.visible = false
      this.$refs.attrGroup.resetFields()
    }
  },
  created(){
    this.tree();
  }
};
</script>
